<?php

class Front_BoutiqueController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $storage = new Zend_Auth_Storage_Session();
    	$data = $storage->read();
    	if(!$data){
    		$this->_redirect('front/auth/login');
    	}
    	$this->view->username = $data->username;
    }

    public function addAction()
    {
    	$request = $this->getRequest();
        $form = new Front_Form_Boutique_Deposer();
        
        //recupérer l'id dela personne connectée
        $storage = new Zend_Auth_Storage_Session();
        $data = $storage->read();
        $user_id = $data->id;
        
        //création de la liste déroulante
        $cat = new Admin_Model_DbTable_Categories();
        $cats = $cat->getAllCaterories();
        $catList = array(''=>'');
        foreach ($cats as $categ){
        	$catList[$categ->id] = $categ->name;
        }
        $form->categorie->setMultiOptions($catList);
     
        $this->view->form = $form;
        
		if ($request->isPost()){      	
        	$formData = $request->getPost();
//         	var_dump($formData); die('tst');
        	if ($form->isValid($formData)){
        		
        		$name = $form->getValue('name');
        		$qtt = $form->getValue('quantite');
        		$price = $form->getValue('prix');
        		$categorie = $form->getValue('categorie');
        		$description = $form->getValue('description');
        		$user_id = $data->id;
        		
        		$produit = new Front_Model_DbTable_Produit();       		
        		$idProduit = $produit->addProduit($name, $qtt, $price, $categorie, $description, $user_id);

        		$upload = new Zend_File_Transfer_Adapter_Http();
        		$renameFile = $idproduit.'.jpg';
        		
        		$fullFilePath = './images/produits/'.$renameFile;
        		
        		$filterFileRename = new Zend_Filter_File_Rename(array('target' => $fullFilePath, 'overwrite' => true));
        		$upload->addFilter($filterFileRename);
        		$upload->setDestination('./images/produits');
        		try{
        			$upload->receive();
        		}catch (Zend_File_Transfer_Exception $e){
        			echo $e->getMessage();
        		}
        		
        		$this->_redirect('front/boutique/index');
        	} else{
        		
        		$form->populate($formData);
        	}
        	
        }
        
    }

    public function editeAction()
    {
        // action body
    }


}
